﻿$(document).ready(function() {
    var toggle = $('#exif_toggle');
    toggle.click(function() {
        if (toggle.attr("class") == "exif") {
            toggle.attr("class", "exif_on");
        }
        else {
            toggle.attr("class", "exif");
        }
        $('#exif_info').slideToggle();
    });
    $('#span_photo_desc').mouseover(function() {

        $(this).addClass("span_focus");
    });
    $('#span_photo_desc').mouseout(function() {

        $(this).removeClass("span_focus");
    });

    $('img.SPRITE_xbox').click(deltag);
    $('#btn_addphototags').click(addtag);
    $('#div_add_tag').click(function() { $('#div_addphototags').fadeIn(); });
    if (_LAT && _LNG) {
        $('#map_canvas').show();
        initialize();
        
    }
});
function checkCommentForm(){
    form = $('#form_addcomment');
    textarea = $('#commentBox');
    if (jQuery.trim(textarea.val()) == "") {
        textarea.addClass("error");
        return false;
    }
    else {
        textarea.removeClass("error");
        return true;
    }
};
    deltag = function(){
            var me = $(this); 
            $.post(_URL_DELETEPHOTOTAG, 
            {photoid:_photoId, tag:me.prev('a').text() }, 
            function(ret) {               
                if (ret == true) {
                    me.parent("li").remove();                    
                    }
                    else
                    {                       
                        alert('删除标记失败');
                    }
               
            }, "json");
    };
    addtag = function(){
        tag = $('#ipt_tags').val();
        tag = jQuery.trim(tag);
        tags = tag.replace('　',' ');
        tags = tags.split(' ');
        
        if(tag == '')
        {           
            $('#ipt_tags').addClass('input-validation-error');
            return;
        }
        else
        {
         $('#ipt_tags').removeClass('input-validation-error');
             $.post(_URL_ADDPHOTOTAGS, 
            {photoid:_photoId, tags: tag }, 
            function(ret) {               
                if (ret == true) {                  
                      var tag_url = $('#div_addphototags').next('ul');   
                      for(i=0;i<tags.length;i++)
                      {
                        var a_tag = $('<a href="#"></a>');
                        a_tag.text(tags[i]);                        
                        img_del = $('<img class="inline-block SPRITE_xbox" title="从该图片删除此标记" style="-moz-user-select: none;" />');
                        img_del.attr('src',_IMG_CLEAR);
                        img_del.click(deltag);
                        li = $('<li></li>');
                        li.append(a_tag);
                        li.append(img_del);
                        tag_url.append(li);
                      }           
                      $('#div_addphototags').fadeOut();
                      $('#ipt_tags').val('');
                    }
                    else
                    {                       
                        alert('添加标记失败');
                    }
               
            }, "json");
        }
    };
    editdesc = function(hasvalue) {
        var input = $('<textarea name="text_photo_desc" class="text_photo_desc"></textarea> ');
        var button = $('<button class="submitBtn" value="保  存"><span>保  存</span></button>')
        var cancel = $('<button class="submitBtn" value="取　消"><span>取　消</span></button>')
        var span = $('<span id="span_photo_desc" onclick="editdesc()"></span>');
        var me = $('#span_photo_desc')
        var div = me.parent('div');
        div.append(button);
        div.append(cancel);
        if(hasvalue)
            input.val(me.html());
        me.replaceWith(input);
        input.focus();
        button.click(function() {
            div.html('');
            div.append(span);

            $.post(_URL_EDITPHOTODESC, {id:_photoId, desc: input.val() }, function(ret) {
           
                if (ret == true) {
                    if (jQuery.trim(input.val()) == "")
                        div.html('<em id="span_photo_desc" onclick="editdesc(false)">点击此处为照片添加描述</em>');
                    else
                    {                       
                        div.html('<span id="span_photo_desc" onclick="editdesc(true)">'+input.val()+'</span>');
                    }
                }
                else
                    alert('修改照片描述出错');
            }, "json");
        });
        cancel.click(function() { div.html(''); div.append(me) });
    };
    
     edittitle = function() {
        var input = $('<input name="text_photo_title" class="text_photo_title" type="text" /> ');
        var button = $('<button class="submitBtn" value="保  存"><span>保  存</span></button>')
        var cancel = $('<button class="submitBtn" value="取　消"><span>取　消</span></button>')
        var span = $('<h2 id="h2_photo_title" onclick="edittitle()"></span>');
        var me = $('#h2_photo_title')
        var div = me.parent('div');
        div.append(button);
        div.append(cancel);
       
        input.val(jQuery.trim(me.text()));
        me.replaceWith(input);
        input.focus();
        button.click(function() {
            div.html('');
            div.append(span);
            if(jQuery.trim(input.val()) == "")
            {
                alert('请为照片填写一个标题');
                return;
            }
            $.post(_URL_EDITPHOTOTITLE, {id:_photoId, title: input.val() }, function(ret) {
           
                if (ret == true) {                                  
                        div.html('<h2 id="h2_photo_title" onclick="edittitle()">'+input.val()+'</h2>');
                    
                }
                else
                    alert('修改照片标题出错');
            }, "json");
        });
        cancel.click(function() { div.html(''); div.append(me) });
    };
//gmaps
if (_LAT && _LNG) {
        google.load('search', '1');
        google.load('maps', '2', { "language": "zh_CN" });
        google.load("search", "1", { "nocss": true });
        function initialize() {
            if (GBrowserIsCompatible()) {
                // Create and Center a Map
                map = new google.maps.Map2(document.getElementById("map_canvas"));
                map.setCenter(new GLatLng(_LAT, _LNG), 10);
                map.addControl(new GSmallZoomControl());
                map.enableScrollWheelZoom();
                var marker = new GMarker(new GLatLng(_LAT, _LNG));
                map.addOverlay(marker);

            }
        }
    }
    savePos = function(plat, plng) {
        if (plat == 0 && plng == 0) {
            tb_remove();
            return;
        }
        $.post(_URL_UPDATEPHOTOLATLNG,
            { photoId: _photoId, lat: plat, lng: plng },
            function(ret) {
                if (ret == true) {
                    tb_remove();
//                    if (!map) {
//                        initialize();
//                    }
                    if (map) {
                        map.clearOverlays();
                        var latlng = new GLatLng(plat, plng);
                        map.panTo(latlng);
                        var marker = new GMarker(latlng);
                        map.addOverlay(marker);
                    }
                }
                else {
                    alert('标记位置失败');
                }
            }, "json");

    };